package net.sashag.nadlandroid;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;

import com.google.android.apps.analytics.easytracking.TrackedPreferenceActivity;
import com.googlecode.androidannotations.annotations.Bean;
import com.googlecode.androidannotations.annotations.EActivity;
import com.parse.ParseUser;

@EActivity
public class NadlandroidPreferencesActivity extends TrackedPreferenceActivity {
	
	@Bean protected FavoritesManager favoritesManager;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setTitle("Settings");
		addPreferencesFromResource(R.xml.preferences);
		
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
		preferences.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
			public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
				Log.v("NadlandroidPreferences", "Shared preferences changed within the activity, key = " + key);
				if (key.equals("useProximityAlerts")) {
					if (sharedPreferences.getBoolean("useProximityAlerts", true)) {
						favoritesManager.setProximityAlertsFromFavoriteApartments(ParseUser.getCurrentUser());
					} else {
						favoritesManager.removeAllProximityAlerts(ParseUser.getCurrentUser());
					}
				}
			}
		});
	}
}
